package com.situ.javase.day9;

import java.util.Arrays;

import org.junit.Test;

public class HomeWork {

	@Test
	public void test00() {
		String str = "----";
		String result = trim(str);
		System.out.println(result);
	}

	private String trim(String str) {
		int startIndex = 0;
		int endIndex = str.length() - 1;
		if (str == null || str.equals("")) {
			return "";
		}
		
		while (startIndex <= endIndex && str.charAt(startIndex) == '-') {
			startIndex++;
		}
		while (startIndex <= endIndex && str.charAt(endIndex) == '-') {
			endIndex--;
		}
		
		String result = str.substring(startIndex, endIndex + 1);
		System.out.println(result);
		
		if (startIndex > endIndex) {
			System.out.println("全是空格");
		} else if (startIndex < endIndex) {
			System.out.println("大于2个字符");
		} else if (startIndex == endIndex) {
			System.out.println("只有一个字符");
		}
		
		return result;
	}
	
	@Test
	public void test001() {
		String str = "2e";
		char[] cha = str.toCharArray();
		int end = cha.length;
		int start = 0;
		while ((start < end) && (cha[start] == '-')) {
			start++;
		}
		while ((start < end) && (cha[end - 1] == '-')) {
			end--;
		}
		System.out.print(((start > 0) || (end < cha.length)) ? str.substring(start, end) : this);
		if(str.substring(start, end).equals("")) {
			System.out.println("全是-");
		}
	}

	@Test
	public void test9() {
		int p = 0;
		int q = 0;
		String str = "  e";
		char[] newString = new char[] {};
		newString = str.toCharArray();
		for (int i = 0; i < newString.length; i++) {
			if (newString[i] == ' ') {
				p = p + 1;
			} else {
				break;
			}
			//卫操作
			if (p == newString.length) {
				System.out.println("");
				return;
			}
		}
		for (int j = newString.length - 1; j > 0; j--) {
			if (newString[j] == ' ') {
				q = q + 1;
			} else {
				break;
			}
		}
		String newsString2 = new String(newString);
		System.out.println(newsString2.substring(p, newString.length - q));
	}

	// 实现把“I Love Java！”的字符全部转换为小写并输出到控制台。
	@Test
	public void test3() {
		String str = "I Love Java";
		char[] array = str.toCharArray();
		for (int i = 0; i < array.length; i++) {
			if (array[i] >= 'A' && array[i] <= 'Z') {
				array[i] = (char)(array[i] + 32);
			}
		}
		System.out.println(Arrays.toString(array));
		// [i,  , l, o, v, e,  , j, a, v, a]
		String s = new String(array);
		System.out.println(s);
	}
	
//	3、使用String类中的split()函数，统计出“this is my homework! I must finish it!”中单词的个数。
//	（注意：单词之间用一个空格来分隔。）
	@Test
	public void test44() {
		String str = "this is my homework! I must finish it!";
		String[] array = str.split(" ");
		System.out.println(array.length);
	}
	
}
